layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer;
        $ = layui.jquery;

    var aCode = [];
    $(function(){
        // var show_num = [];
        draw();

        $("#canvas").on('click',function(){
            draw();
        });
    });

    // 表单验证
    // $(".btn").on('click',function(){
    //     var val = $(".input-val").val().toLowerCase();
    //     var num = show_num.join("");
    //     if(val==''){
    //         alert('请输入验证码！');
    //     }else if(val == num){
    //         alert('提交成功！');
    //         $(".input-val").val('');
    //         draw(show_num);
    //
    //     }else{
    //         alert('验证码错误！请重新输入！');
    //         $(".input-val").val('');
    //         draw(show_num);
    //     }
    // })

    function draw() {
        $.ajax({
            type:"POST",
            url:'/login/imgCode',
            async:false,
            success:function(res){
                // console.log(res);
                aCode = res;
            }
        });

        var canvas_width=$('#canvas').width();
        var canvas_height=$('#canvas').height();
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width + 1;
        canvas.height = canvas_height + 1;

        for (var i = 0; i <= 3; i++) {
            // var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            // var txt = aCode[j];//得到随机的一个内容
            var txt = aCode[i];//得到随机的一个内容
            // show_num[i] = txt.toLowerCase();
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";

            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }

    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    $(".loginBody .seraph").click(function(){
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
            time:5000
        });
    });

    //登录按钮
    form.on("submit(login)",function(data){

        //1、获得手机号
        var username = $("#userName").val().trim();
        var password = $("#password").val().trim();
        var code = $("#code").val().trim();
        //2、检验手机号
        if(!(/^1[3456789]\d{9}$/.test(username))) {
            layer.alert("手机号码错误，请重填！");
            return false;
        }

        if(code.length != 4) {
            layer.alert("验证码长度错误，请重填！");
            return false;
        }

        for(var i = 0;i < 4;i++) {
            if(code.charAt(i) != aCode[i]) {
                layer.alert("验证码错误，请重填！");
                return false;
            }
        }

        layer.msg('正在登陆，请稍后！',{icon:1,time:800,shade:[0.6,'#000',true]});
        setTimeout(function(){
            //4、对密码进行加密
            var md5password = hex_md5("jgsu" + password + "c611");
            // alert(md5password)
            //5、发送ajax请求，进行登陆
            $.ajax({
                type:"POST",
                url:'/login/login',
                dataType: "json",
                data: { username: username, password: md5password,code: code },
                async:false,
                success:function(res){
                    if(res == 1) {
                        //6、进行登陆，跳转
                        window.location.href = "/index.html";
                    }else {
                        layer.alert("没有此用户，登陆失败！");
                    }
                }
            });

        },1000);
        return false;
    });

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    });
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
